package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC927 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

//        public Map<Pair<Integer, Integer>, Long> memo = new HashMap<>();

        public int[] threeEqualParts(int[] arr) {
            if (arr.length < 3) return new int[]{-1, -1};

            long left = 0, middle = 0, right = 0;
            for (int i = 0; i < arr.length - 2; i++) {
                left = (left << 1) + arr[i];
                middle = 0;
                for (int j = i + 2; j < arr.length; j++) {
                    middle = (middle << 1) + arr[j - 1];
                    if (j == i + 2) {
                        //新计算
                        right = 0;
                        for (int k = j; k < arr.length; k++) {
                            right = (right << 1) + arr[k];
                        }
                    } else {
                        right = right - (arr[j - 1] << (arr.length - j));
                    }

                    if (left == middle && middle == right) return new int[]{i, j};
                    if (left < 0 || middle < 0 || right < 0) {
                        System.out.println(i + ", " + j);
                    }
                }
            }

            return new int[]{-1, -1};
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0]");
        System.out.println(Arrays.toString(solution.threeEqualParts(arr)));
    }

    public static void main(String[] args) throws Exception {
        LC927 an = new LC927();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
